<?php
$GLOBALS['Md'] = "FTK9D4D0";
$GLOBALS['Pd'] = "G";
require_once($_SERVER['DOCUMENT_ROOT']."/defaultconf.mex");
MYDCONN();
if (isset($_POST['UpdateValues']) && $_POST['UpdateValues'] == "Actualizar") {

	$Sum = $_POST['Sum'];
	$Ref = $_POST['RefOffice'];
	$OperationTime = date("Y-m-d H:i:s");
	$Cord = count($Sum);
	for ($r = 0; $r < $Cord; $r++) {
	
	$QUpdate = "SELECT Sum FROM staff 
	WHERE RefOffice = {$Ref[$r]}
	ORDER BY Time DESC LIMIT 0,1";
	$Update = $GLOBALS['MYD']->db_array($QUpdate); 
		if (!isset($Update[1]) || $Update[1]['Sum'] != $Sum[$r]) {
		$QOrd = "INSERT INTO staff (Time,RefOffice,Sum) VALUES 
		('{$OperationTime}',{$Ref[$r]},'{$Sum[$r]}')";
		$Ord = $GLOBALS['MYD']->db_query($QOrd); 
		}	
	}
	$Msg_CONFIRMATION = "Dados actualizados com sucesso.";

}
?>
<table width="100%"  border="0" cellpadding="2" cellspacing="2">
  <tr>
    <td>
      <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
	<table width="100%"  border="0" cellpadding="2" cellspacing="1">

        <tr bgcolor="#99CCCC">
          <td bgcolor="#99CCCC">Loja</td>
          <td bgcolor="#99CCCC">Funcion�rios</td>
          <td bgcolor="#99CCCC">Data</td>
        </tr>
    <?php 
	$QCallOffice = "SELECT main_offices.ref,
							main_offices.name
						    FROM main_offices 
							WHERE ref_domain = 1 
							AND Type IN (1, 2)
							ORDER BY name ASC";
	$CallOffice = $GLOBALS['MYD']->db_array($QCallOffice); 
	for ($o = 1; $o <= $CallOffice[0]; $o++) {
	
			$QCallValues = "SELECT staff.Time,
									staff.Sum
						    FROM staff
							WHERE staff.RefOffice = {$CallOffice[$o]['ref']} 
							ORDER BY Time DESC LIMIT 0,1";
			$StaffTotal = $GLOBALS['MYD']->db_array($QCallValues);
	?>
    <tr bgcolor="#F2F2F2">
      <td><input name="RefOffice[]" type="hidden" value="<?php echo $CallOffice[$o]['ref'];?>"><?php echo $CallOffice[$o]['name'];?></td>
      <td><input name="Sum[]" type="text" id="Sum[]" value="<?php echo $StaffTotal[1]['Sum'];?>" size="4" maxlength="10"></td>
      <td><?php echo $StaffTotal[1]['Time'];?></td>
    </tr>
    <?php } ?>
    <tr bgcolor="#F2F2F2">
      <td colspan="3"><input name="UpdateValues" type="submit" id="UpdateValues" value="Actualizar"></td>
      </tr>
      
    </table></form></td>
  </tr>
</table>